#ifndef DATAFLOW_H
#define DATAFLOW_H


#include <string>
//#include <list>

//using std::list;
using std::string;

class DataFlow
{
public:
    DataFlow(): BufferLength(0), Buffer(NULL) { }
    DataFlow(const string s)
    {
        SetData(s.data(), s.length() + 1);
    }
    DataFlow(const char* s)
    {
        SetData(s, s ? strlen(s) + 1 : 0);
    }
    DataFlow(const long int n)
    {
        SetData((char *)&n, sizeof(long int));
    }
    DataFlow(const DataFlow &orig)
    {
        SetData(orig.Buffer, orig.BufferLength);
    }
    DataFlow(const char* buffer, unsigned int length)
    {
        SetData(buffer, length);
    }

    DataFlow & operator = ( const DataFlow &rhs )
    {
		ClearBuffer();
		SetData(rhs.Buffer, rhs.BufferLength);
        return *this;
    }
    ~DataFlow()
    {
        ClearBuffer();
    }
    void ClearBuffer()
    {
        if ((BufferLength > 0 ) && (Buffer))
        {
            BufferLength = 0;
            delete [] Buffer;
            Buffer = NULL;
        }

    }
    void SetData(const char* buffer, unsigned int length)
    {
        if ((buffer) && (length > 0))
        {
            BufferLength = length;
            Buffer = new char[BufferLength];
            memcpy(Buffer, buffer, length);
        }
        else
        {
            BufferLength = 0;
            Buffer = NULL;
        }
    }
    char * GetData() const
    {
        return Buffer;
    }
	char * GetListData(int num, int size) const
	{
		if ( ((unsigned)size * num >= BufferLength) ||
			(!Buffer) ) return NULL;
		return Buffer + size * num;
	}
	int GetListNumber(int size) const
	{
		if (BufferLength % size != 0) return -1;
		return BufferLength / size;
	}
	template <typename T>
	bool GetNumber(T &n)
	{
		long int i;
		if (!Buffer) return 0;
		memcpy(&i, Buffer, BufferLength);
		n = (T)i;
		return 1;
	}
	bool GetNumber(long int *i)
	{
		if (!Buffer) return 0;
		memcpy(i, Buffer, BufferLength);
		return 1;
	}
    unsigned int GetLength() const
    {
        return BufferLength;
    }

protected:
    //bool changed;
    unsigned int BufferLength;
    char *Buffer;
};

















#endif